/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.framework.excel.config;

import cn.hutool.core.collection.CollectionUtil;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * 读写Excel常量类<br>
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
public final class ReadWriteConstant {

  public static Map<Boolean, List<String>> getBooleanValues() {
    Map<Boolean, List<String>> map = new HashMap<>(2);

    map.put(true, CollectionUtil.toList("是", "TRUE"));
    map.put(false, CollectionUtil.toList("否", "FALSE"));

    return map;
  }

  public static int getBooleanValuesIndex(
      String[] labels, Map<Boolean, List<String>> booleanValues) {
    int index = -1;
    // 以下两个List长度肯定是相等，否则为非法数据
    List<String> tValues = booleanValues.get(true), fValues = booleanValues.get(false);

    for (String label : labels) {
      index =
          tValues.indexOf(
              tValues.stream()
                  .filter(v -> label.toUpperCase(Locale.ROOT).contains(v))
                  .findFirst()
                  .orElse(""));
      if (index >= 0) {
        break;
      }
      index =
          fValues.indexOf(
              tValues.stream()
                  .filter(v -> label.toUpperCase(Locale.ROOT).contains(v))
                  .findFirst()
                  .orElse(""));

      if (index >= 0) {
        break;
      }
    }

    return index == -1 ? tValues.size() - 1 : index;
  }
}
